package com.jackrain.nea.fc.dl.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.common.QueryCpUtil;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.OmsDlShareStockQueryCmd;
import com.jackrain.nea.fc.dl.mapper.OmsDlShareStockMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;

/**
 * 共享库存 全量
 * @author kangcong
 * @date 2018/09/28
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "dl")
public class OmsDlShareStockQueryCmdImpl  extends CommandAdapter implements OmsDlShareStockQueryCmd {

    @Autowired
    QueryCpUtil queryCpUtil;

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
//        log.debug("--------------共享同步到OMS全量传输---start------------");
        ValueHolder vh = new ValueHolder();
        log.info("OmsDlShareStockQueryCmdImpl--map-->{}",map);

        OmsDlShareStockMapper omsDlShareStockMapper = ApplicationContextHandle.getBean(OmsDlShareStockMapper.class);
        JSONArray jsonArray = new JSONArray();

        //获取并处理库存数据
        List<HashMap> listData = omsDlShareStockMapper.selectDlShareStock();
        for (HashMap hashMap : listData) {

            JSONObject js = new JSONObject(hashMap);
            JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(js,
                    "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArray.add(param1);
        }
//        log.debug("--------------共享同步到OMS全量传输---end------------"+jsonArray);
        log.debug("OmsDlShareStockQueryCmdImpl--end-->{}",jsonArray);

        vh.put("data", jsonArray);
        vh.put("code", 0);
        vh.put("message", "查询成功！");
        return vh;
    }
}
